package com.jmcgg.freesky.common.util;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.jmcgg.freesky.common.db.DBConnBean;

/**
 * Title: FreeSky Common Java Library <br>
 * Description: FreeSky Common Java Library <br>
 * Copyright: Copyright (c) 2005~2009 www.afss.cn <br>
 * Company: Amei's FreeSky Studio <br>
 * 
 * @author <b>Jacky Chou</b> (AFSS [Amei's FreeSky Studio])
 * @version 1.2.1, 2008-08-05, Clean and optimize the source code.<br>
 *          1.2.0, 2008-02-23, Upgrade freesky.jar to 1.2.0 with Struts 2<br>
 *          1.0.0, 2006-05-30, Initial Version
 */
public class DBLogUtil extends FreeskyUtil {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(DBLogUtil.class);

	public static DBConnBean FS_LOG_CONN = null;

	public DBLogUtil() {
	}

	private static String sql = "INSERT INTO FS_LOG(LOG_LEVEL,LOG_TYPE,LOG_MODULE,LOG_MSG,LOG_TIME) Values(?,?,?,?,?)";

	public static String FS_LOG_LEVEL = "DEBUG";

	public static void debug(String msg) {
		log(0, "DEBUG", "AFSS - FREESKY", msg);
	}

	public static void info(String msg) {
		log(1, "INFO", "AFSS - FREESKY", msg);
	}

	public static void warn(String msg) {
		log(2, "WARN", "AFSS - FREESKY", msg);
	}

	public static void error(String msg) {
		log(3, "ERROR", "AFSS - FREESKY", msg);
	}

	public static void fatal(String msg) {
		log(4, "FATAL", "AFSS - FREESKY", msg);
	}

	public static void debug(String module, String msg) {
		log(0, "DEBUG", module, msg);
	}

	public static void info(String module, String msg) {
		log(1, "INFO", module, msg);
	}

	public static void warn(String module, String msg) {
		log(2, "WARN", module, msg);
	}

	public static void error(String module, String msg) {
		log(3, "ERROR", module, msg);
	}

	public static void fatal(String module, String msg) {
		log(4, "FATAL", module, msg);
	}

	private static void log(int logLevel, String logType, String module,
			String msg) {
		if (logLevel >= getLogLevel()) {
			try {
				if (FS_LOG_CONN == null)
					return;
				PreparedStatement pstmt = FS_LOG_CONN.prepareStatement(sql);
				pstmt.setInt(1, logLevel);
				pstmt.setString(2, logType);
				pstmt.setString(3, module);
				pstmt.setString(4, msg);
				pstmt.setString(5, DateUtil.getDateTime());
				pstmt.execute();
				pstmt.close();
			} catch (SQLException e) {
				logger.error("log(int, String, String, String) - int logLevel="
						+ logLevel + ", String logType=" + logType
						+ ", String module=" + module + ", String msg=" + msg,
						e);
			}
		}
	}

	private static int getLogLevel() {
		if (FS_LOG_LEVEL.equalsIgnoreCase("DEBUG"))
			return 0;
		else if (FS_LOG_LEVEL.equalsIgnoreCase("INFO"))
			return 1;
		else if (FS_LOG_LEVEL.equalsIgnoreCase("WARN"))
			return 2;
		else if (FS_LOG_LEVEL.equalsIgnoreCase("ERROR"))
			return 3;
		else if (FS_LOG_LEVEL.equalsIgnoreCase("FATAL"))
			return 4;
		else
			return 3; // set default is debug
	}
}
